const app = getApp()
const auth = require('../../utils/auth.js')

Page({
  data: {
    userInfo: {},
    profile: {
      bio: '分享我的社交账号'
    },
    links: [],
    isOwner: false
  },

  onLoad(options) {
    // 检查登录状态
    if (!auth.checkLogin()) return

    // 如果有分享参数，加载对应用户的链接
    if (options.userId) {
      this.loadUserLinks(options.userId)
    } else {
      // 加载当前用户的链接
      this.setData({
        isOwner: true,
        userInfo: app.globalData.userInfo,
        profile: app.globalData.profile,
        links: app.globalData.links
      })
    }
  },

  loadUserLinks(userId) {
    // TODO: 替换为实际的服务器接口地址
    const apiUrl = `https://your-api-domain.com/api/user/${userId}/links`
    
    wx.request({
      url: apiUrl,
      method: 'GET',
      success: (res) => {
        if (res.data.success) {
          this.setData({
            userInfo: res.data.userInfo,
            profile: res.data.profile,
            links: res.data.links
          })
        }
      }
    })
  },

  openLink(e) {
    const url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url
    })
  },

  navigateToEdit() {
    wx.navigateTo({
      url: '/pages/edit/edit'
    })
  },

  navigateToProfile() {
    wx.navigateTo({
      url: '/pages/profile/profile'
    })
  },

  onShareAppMessage() {
    return {
      title: this.data.userInfo.nickName + '的社交账号主页',
      path: '/pages/index/index?userId=' + app.globalData.userId
    }
  }
}) 